const app = getApp();
const md = require('./md5.js');

function getdata(url,data,callback,spec=''){
  // console.log(data);
  
  var that = this;
  // 获取时间戳
  var apinameIsMeb = url;
  var timestamp = new Date().getTime();
  timestamp = parseInt(timestamp / 1000);
  var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
  data.sign = md5IsMeb;
  data.appid = app.server.appid;
  data.tmsp = timestamp;
  wx.request({
    //后台接口地址
    url: app.server.url + apinameIsMeb,
    data: data,
    method: 'POST',
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    success: function (res) {
      // console.log(res);
      if(res.data.code == 0) {
        callback(res);
      }else{
        if(spec!=''){
          spec(res);
          return;
        }else{
          wx.hideLoading();
          let flg = res.data.msg.indexOf("请重新运行该事务");
          if(flg != -1){
            wx.showModal({
              title: '',
              content:'网络错误。请重试！',
              success:function(res){
                let depid = wx.getStorageSync('depid');
                that.getalldata(depid);
              }
            })
          }else{
            wx.showModal({
              title: '',
              content: res.data.msg,
            })
          }
          
          console.log(res);
          return;
        }
       
      }
    },
    fail:function(res){
      console.log(res)
      wx.hideLoading();
      wx.showModal({
        title: '',
        content:"error："+ res.data.msg,
      })
      return;
    }
  })
}
function toDecimal2(x) {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return false;
  }
  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  s = parseFloat(s);
  return s;
} 
module.exports = {
  getdata: getdata,
  toDecimal2: toDecimal2
}